package com.caiwm.view.panel;

import com.caiwm.common.ServerResponse;
import com.caiwm.entity.Student;
import com.caiwm.service.impls.StudentService;
import com.caiwm.view.StudentView;
import com.caiwm.view.dialog.CaiwmDialog;
import com.caiwm.view.table.StudentTableModel;
import com.github.pagehelper.PageInfo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃  神兽保佑
 * 　　　　┃　　　┃  代码无bug
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 * </pre>
 *
 * @author shiqla
 * @Desc 学生信息管理系统主操作界面
 * @Date 2019/1/7/007
 */
public class DataViewPanel extends CaiwmDialog implements ActionListener {

    private JPanel jPanel;

    private StudentView studentView;

    private StudentService studentService;

    private Integer pageNum = 1;

    private Integer pageSize = 20;

    private MenuPanel menuPanel;

    private JTable jTable;


    // 分页相关的数据
    private JPanel paginationPanel;

    private StudentTableModel studentTableModel;

    private PageInfo pageInfo;

    private StringBuilder pageNumStr;

    private JLabel pageNumStrLabel;

    private JPanel pageNumsPanel;

    private JPanel searchPanel;

    // 查询按钮栏
    private JTextField idField;

    private JTextField nameField;

    private JComboBox gradeComboBox;

    private JComboBox classComboBox;

    private JButton searchBtn;

    private JButton resetBtn;

    private JButton refushBtn;

    public DataViewPanel(StudentView studentView) {
        this.studentView = studentView;
        jPanel = new JPanel();
        studentService = new StudentService();

        String[] rowName = {"学号", "姓名", "年龄", "性别", "联系电话", "父母联系电话", "父母名称", "居住地址", "年级", "班级"};
        this.studentTableModel = new StudentTableModel(new Object[0][11], rowName);
        this.jTable = new JTable(this.studentTableModel);

        paginationPanel = new JPanel();
        pageNumStr = new StringBuilder();
        pageNumStrLabel = new JLabel();

        searchPanel = new JPanel();
    }

    private void getStudentData() {
        ServerResponse serverResponse = studentService.selectByCondition(this.pageNum, this.pageSize, null, null, null, null, null);
        this.pageInfo = (PageInfo) serverResponse.getData();
    }

    public JPanel renderTablePanel() {

        this.jPanel.setBackground(new Color(219, 19, 19));

        this.getStudentData();
        this.setTableData(pageInfo.getList());

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(this.jTable);
        this.jPanel.add(jScrollPane);
        this.jPanel.setLayout(new GridLayout(1, 1));
        return this.jPanel;
    }

    private void setTableData(List<Student> list) {
        Integer rowIndex = 0;
        this.studentTableModel.setRowCount(0);
        for (Student student : list) {
            this.studentTableModel.insertRow(rowIndex++, this.objectConvert(student));
        }
    }

    private Object[] objectConvert(Student student) {
        List<Object> objectList = new ArrayList<Object>();
        objectList.add(student.getId());
        objectList.add(student.getName());
        objectList.add(student.getAge());
        objectList.add(student.getSex());
        objectList.add(student.getPhone());
        objectList.add(student.getParentPhone());
        objectList.add(student.getParentName());
        objectList.add(student.getAddress());
        objectList.add(student.getGrade());
        objectList.add(student.getStudentClass());

        return objectList.toArray();
    }

    private String getPageNumStr() {

        Integer pageCounts = this.pageInfo.getPages();

        long total = this.pageInfo.getTotal();

        String str = "当前页码:" + this.pageNum + "/" + pageCounts + "  总记录数:" + total;

        return str;
    }

    public JPanel paginationRender() {

        this.pageNumStr.append(this.getPageNumStr());

        pageNumStrLabel.setText(this.pageNumStr.toString());
        JButton firstBtn = new JButton("第一页");
        JButton lastBtn = new JButton("最后一页");
        JButton preButton = new JButton("前一页");
        JButton nextButton = new JButton("后一页");

        firstBtn.addActionListener(this);
        preButton.addActionListener(this);
        nextButton.addActionListener(this);
        lastBtn.addActionListener(this);

        this.paginationPanel.add(firstBtn);
        this.paginationPanel.add(preButton);
        this.paginationPanel.add(nextButton);
        this.paginationPanel.add(lastBtn);

        this.paginationPanel.add(pageNumStrLabel);
        this.paginationPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        return this.paginationPanel;
    }

    public JPanel renderSearchPanel() {

        JLabel idLabel = new JLabel("学号:");
        idField = new JTextField(10);

        JLabel nameLabel = new JLabel("姓名:");
        nameField = new JTextField(10);

        JLabel gradeLabel = new JLabel("年级:");
        String[] grades = {"一年级", "二年级", "三年级", "四年级", "五年级", "六年级"};
        gradeComboBox = new JComboBox(grades);

        JLabel classLabel = new JLabel("班级:");
        String[] classes = {"一班", "二班", "三班", "四班", "五班", "六班"};
        classComboBox = new JComboBox(classes);

        searchBtn = new JButton("按条件查询");
        resetBtn = new JButton("重置条件");
        refushBtn = new JButton("刷新");

        this.searchBtn.addActionListener(this);
        this.resetBtn.addActionListener(this);
        this.refushBtn.addActionListener(this);

        this.searchPanel.add(idLabel);
        this.searchPanel.add(idField);

        this.searchPanel.add(nameLabel);
        this.searchPanel.add(nameField);

        this.searchPanel.add(gradeLabel);
        this.searchPanel.add(gradeComboBox);

        this.searchPanel.add(classLabel);
        this.searchPanel.add(classComboBox);

        this.searchPanel.add(searchBtn);
        this.searchPanel.add(resetBtn);
        this.searchPanel.add(refushBtn);

        this.searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        return this.searchPanel;
    }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (command.equals("第一页")) {
            this.pageNum = this.pageInfo.getFirstPage();
            this.pageNumStrLabel.setText(this.getPageNumStr());
            this.getStudentData();
            this.setTableData(this.pageInfo.getList());

        } else if (command.equals("前一页")) {
            if (this.pageInfo.isHasPreviousPage()) {
                this.pageNum = this.pageInfo.getPrePage();
                this.pageNumStrLabel.setText(this.getPageNumStr());
                this.getStudentData();
                this.setTableData(this.pageInfo.getList());
            } else {
                this.successMessageDialog("已经是第一页,无法继续翻页");
            }
        } else if (command.equals("后一页")) {
            if (this.pageInfo.isHasNextPage()) {
                this.pageNum = this.pageInfo.getNextPage();
                this.pageNumStrLabel.setText(this.getPageNumStr());
                this.getStudentData();
                this.setTableData(this.pageInfo.getList());
            } else {
                this.successMessageDialog("已经是最后一页,无法继续翻页");
            }
        } else if (command.equals("最后一页")) {
            this.pageNum = this.pageInfo.getLastPage();
            this.pageNumStrLabel.setText(this.getPageNumStr());
            this.getStudentData();
            this.setTableData(this.pageInfo.getList());
        } else if (command.equals("按条件查询")) {

            String id = this.idField.getText();
            Integer integerId = null;
            if (id != null && !id.equals("")) {
                integerId = Integer.valueOf(id);
            }

            String name = this.nameField.getText();
            if (name.equals("")) {
                name = null;
            }
            String grade = (String) this.gradeComboBox.getSelectedItem();
            String classes = (String) this.classComboBox.getSelectedItem();
            ServerResponse serverResponse = studentService.selectByCondition(this.pageNum, this.pageSize, integerId, name, grade, classes, null);
            this.pageInfo = (PageInfo) serverResponse.getData();
            this.setTableData(this.pageInfo.getList());

        } else if (command.equals("刷新")) {
            this.pageNum = 1;
            this.pageSize = 20;
            this.getStudentData();
            this.pageNumStrLabel.setText(this.getPageNumStr());
            this.setTableData(this.pageInfo.getList());
        } else if (command.equals("重置条件")) {
            this.pageNum = 1;
            this.pageSize = 20;
            this.nameField.setText("");
            this.idField.setText("");
        }
    }

    public JTable getjTable() {
        return jTable;
    }

    public StudentTableModel getStudentTableModel() {
        return studentTableModel;
    }
}
